package io.renren.common.utils;

public class GeoDistance {

    private static final double EARTH_RADIUS = 6371.0; // 地球半径，单位为km

    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        double latDistance = Math.toRadians(lat2 - lat1);
        double lonDistance = Math.toRadians(lon2 - lon2);
        double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
                + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
                * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        return EARTH_RADIUS * c;
    }

    public static void main(String[] args) {
        double lat1 = 40.7128; // 纬度1，例如纽约
        double lon1 = -74.0060; // 经度1
        double lat2 = 34.0522; // 纬度2，例如洛杉矶
        double lon2 = -118.2437; // 经度2

        double distance = calculateDistance(lat1, lon1, lat2, lon2);
        System.out.println("The distance between New York and Los Angeles is " + distance + " km.");
    }
}